#!/bin/bash
# AppImage 打包脚本

set -e

echo "=========================================="
echo "  Notepad-- AppImage 打包"
echo "=========================================="
echo ""

# 检查依赖
if ! command -v wget &> /dev/null; then
    echo "✗ 需要安装 wget"
    exit 1
fi

# 进入构建目录
cd how_build
mkdir -p build
cd build

# 清理
rm -rf AppDir *.AppImage

# CMake 配置
echo "[1/5] CMake 配置..."
cmake -DCMAKE_INSTALL_PREFIX=/usr ..

# 编译
echo ""
echo "[2/5] 编译..."
make -j$(nproc)

# 安装到 AppDir
echo ""
echo "[3/5] 安装到 AppDir..."
make DESTDIR=AppDir install

# 下载 appimagetool
echo ""
echo "[4/5] 下载 appimagetool..."
if [ ! -f appimagetool-x86_64.AppImage ]; then
    wget -q "https://github.com/AppImage/AppImageKit/releases/download/continuous/appimagetool-x86_64.AppImage"
    chmod +x appimagetool-x86_64.AppImage
fi

# 创建 AppImage
echo ""
echo "[5/5] 生成 AppImage..."
./appimagetool-x86_64.AppImage AppDir NotePad--.AppImage

echo ""
echo "=========================================="
echo "  AppImage 打包完成！"
echo "=========================================="
echo ""
ls -lh NotePad--.AppImage
echo ""
echo "使用方式："
echo "  chmod +x NotePad--.AppImage"
echo "  ./NotePad--.AppImage"
echo ""
echo "集成到系统："
echo "  ./NotePad--.AppImage --appimage-extract"
echo "  cd squashfs-root/usr/bin"
echo "  ./install-desktop-integration.sh"
echo ""

